package com.wxw.BaseLibrary.component;

import android.content.Context;
import android.graphics.Color;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wxw.BaseLibrary.tool.ScreenUtils;

import java.util.List;

/**
 * @author david
 */
public class PopMenuGridAdapter extends BaseAdapter {
    private Context _context;
    private List<PopMenuMoreItem> itemList;

    public PopMenuGridAdapter(Context context, List<PopMenuMoreItem> items) {
        this._context = context;
        this.itemList = items;
    }

    @Override
    public int getCount() {
        return itemList == null ? 0 : itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList == null ? null :
                (position < 0 ? null :
                        (position >= itemList.size() ? null : itemList.get(position)));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            ViewHolder holder = new ViewHolder();
            convertView = initView(holder);
        } else if (convertView.getParent() != null) {
            ((ViewGroup) convertView.getParent()).removeView(convertView);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        PopMenuMoreItem item = itemList.get(position);

        if (item.getResId() != 0) {
            holder.icon.setVisibility(View.VISIBLE);
            holder.icon.setImageResource(item.getResId());
        } else {
            holder.icon.setVisibility(View.GONE);
        }
        holder.text.setText(item.getText());
        return convertView;
    }

    private View initView(ViewHolder holder) {
        LinearLayout mainView = new LinearLayout(_context);
        mainView.setBackgroundColor(Color.BLACK);
        mainView.setFocusable(false);
        int padding = ScreenUtils.convertScale(_context, 15);
        mainView.setPadding(padding, padding, padding, padding);
        mainView.setFocusableInTouchMode(false);
        mainView.setOrientation(LinearLayout.HORIZONTAL);
        mainView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        holder.icon = new BaseImageView(_context);
        int iconWidth = ScreenUtils.convertScale(_context, 35);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(iconWidth, iconWidth);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        int margin = ScreenUtils.convertScale(_context, 5);
        layoutParams.setMargins(margin, margin, margin, margin);
        mainView.addView(holder.icon, layoutParams);

        holder.text = new TextView(_context);
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = 1;
        layoutParams.weight = 1;
        holder.text.setClickable(false);
        holder.text.setCompoundDrawablePadding(3);
        holder.text.setGravity(Gravity.CENTER_VERTICAL);
        padding = ScreenUtils.convertScale(_context, 7);
        holder.text.setPadding(padding, padding, padding, padding);
        holder.text.setTextColor(Color.WHITE);
        holder.text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f);
        mainView.addView(holder.text, layoutParams);
        mainView.setTag(holder);
        return mainView;
    }

    private class ViewHolder {
        BaseImageView icon;
        TextView text;
    }
}
